package com.share.device.controller;

import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.device.domain.Cabinet;
import com.share.device.domain.CabinetSlot;
import com.share.device.domain.CabinetVo;
import com.share.device.service.ICabinetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Tag(name = "充电宝柜机接口管理")
@RestController
@RequestMapping("/cabinet")
@RequiredArgsConstructor
public class CabinetController extends BaseController {

    private final ICabinetService cabinetService;

    @Operation(summary = "搜索未使用柜机")
    @GetMapping(value = "/searchNoUseList/{keyword}")
    public AjaxResult searchNoUseList(@PathVariable String keyword) {
        return success(cabinetService.searchNoUseList(keyword));
    }

    //删除
    @Operation(summary = "删除")
    @DeleteMapping("{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        boolean flag = cabinetService.removeBatchByIds(Arrays.asList(ids));
        return toAjax(flag);
    }

    //修改
    @Operation(summary = "修改")
    @PutMapping
    public AjaxResult update(@RequestBody Cabinet cabinet) {
        boolean flag = cabinetService.updateById(cabinet);
        return toAjax(flag);
    }

    //添加
    @Operation(summary = "添加")
    @PostMapping
    public AjaxResult add(@RequestBody Cabinet cabinet) {
        boolean flag = cabinetService.addCabinet(cabinet);
        return toAjax(flag);
    }

    //根据id查询详情
    @Operation(summary = "根据id查询充电宝柜机详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        Cabinet cabinet = cabinetService.getById(id);
        return success(cabinet);
    }

    /**
     * 查询充电宝柜机列表
     */
    @Operation(summary = "查询充电宝柜机列表")
    @GetMapping("/list")
    public TableDataInfo list(Cabinet cabinet) {
        //设置分页参数
        startPage();
        //调用service方法
        List<Cabinet> list = cabinetService.selectListCabinet(cabinet);
        return getDataTable(list);
    }

    @Operation(summary = "获取充电宝柜机全部详细信息")
    @GetMapping(value = "/getAllInfo/{id}")
    public AjaxResult getAllInfo(@PathVariable("id") Long id) {
        return success(cabinetService.getAllInfo(id));
    }

    /* 新增方法供html页面查询 */
    /**
     * 获取所有柜机列表
     */
    @Operation(summary = "获取所有柜机列表")
    @GetMapping("/devices")
    public AjaxResult getAllDevices() {
        List<CabinetVo> devices = cabinetService.getAllCabinets();
        return success(devices);
    }

    /**
     * 获取柜机完整信息（包含槽位）
     */
    @Operation(summary = "获取柜机完整信息（含槽位）")
    @GetMapping("/devices/{cabinetId}")
    public AjaxResult getCabinetWithSlots(@PathVariable Long cabinetId) {
        System.err.println("前端传递参数: " + cabinetId);
        CabinetVo vo = cabinetService.getCabinetWithSlots(cabinetId);
        return success(vo);
    }

    /**
     * 更新槽位状态（租借/归还）
     */
    @Operation(summary = "更新槽位状态")
    @PostMapping("/devices/{cabinetId}/slots/{slotNo}/status")
    public AjaxResult updateSlotStatus(@PathVariable Long cabinetId,
                                       @PathVariable String slotNo,
                                       @RequestBody Map<String, String> body) {
        System.err.println("前端传递参数: " + cabinetId + "><" + slotNo + "><" + body);
        String action = body.get("action"); // rent / return
        CabinetVo vo = cabinetService.updateSlotStatus(cabinetId, slotNo, action);
        return success(vo);
    }

    /**
     * 获取一个可用槽位
     */
    @Operation(summary = "获取一个可用槽位")
    @GetMapping("/devices/{cabinetId}/slots/available")
    public AjaxResult getAvailableSlot(@PathVariable Long cabinetId) {
        System.err.println("前端传递参数: " + cabinetId);
        CabinetSlot slot = cabinetService.getAvailableSlot(cabinetId);
        return success(slot);
    }

    /**
     * 获取已占用的槽位
     */
    @Operation(summary = "获取已占用槽位列表")
    @GetMapping("/devices/{cabinetId}/slots/occupied")
    public AjaxResult getOccupiedSlots(@PathVariable Long cabinetId) {
        System.err.println("前端传递参数: " + cabinetId);
        List<CabinetSlot> slots = cabinetService.getOccupiedSlots(cabinetId);
        return success(slots);
    }

}
